package jsecurity;

import java.nio.charset.Charset;
import java.security.Security;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import jsecurity.asym.KeyPair;
import jsecurity.asym.KeyPair.KeySize;
import jsecurity.cert.X509Certificate;
import jsecurity.encryption.Cipher;
import jsecurity.utils.Base64Codec;

public final class JSecurity
{
	public static Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
	
	public static void changeDefaultCharset(Charset charset)
	{
		if(charset == null) throw new IllegalArgumentException("The jsecurity char set must be well defined");
		
		DEFAULT_CHARSET = charset;
	}
	
	public static void init()
	{
		Security.addProvider(new BouncyCastleProvider());
	}
	
	public static void main(String[] args) throws Exception
	{
		Base64Codec base64 = new Base64Codec();
		String plaintext = "José Lima";
		
		System.out.println("will create the Elliptic Curve Key pair...");
		KeyPair keypair = new KeyPair(KeySize.K1024);
		System.out.println("private key: " + base64.encodeBytes(keypair.getPrivateKey().getEncoded()));
		System.out.println("public  key: " + base64.encodeBytes(keypair.getPublicKey().getEncoded()));
		
		System.out.println("========================");
		System.out.println("text to encrypt: " + plaintext);
		System.out.println("========================");
		
		Cipher cipher = new Cipher();
		
		System.out.println("encrypted: " + cipher.encrypt(plaintext, keypair.getPublicKey()));
		System.out.println("decrypted: " + cipher.decrypt(cipher.encrypt(plaintext, keypair.getPublicKey()), keypair.getPrivateKey()));
		System.out.println("========================");
		
		X509Certificate cert = new X509Certificate(keypair);
		System.out.println(cert);
	}
}
